package com.meishij.xml;

import com.meishij.entry.FindCaiDanEntry;
import com.meishij.entry.FindShiCaiEntry;
import com.meishij.entry.FindZhuanTiEntry;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 阿飞 on 2016/10/13.
 */

public class FindDetailXmlUtils {
    public static FindShiCaiEntry xml2FindShiCaiEntry(String xml) {
        FindShiCaiEntry shiCaiEntry = null;
        List<FindShiCaiEntry.ShiCaiItemEntry> items = null;
        FindShiCaiEntry.ShiCaiItemEntry shiCaiItemEntry = null;
        List<String> gongxiao = null;
        String gongxiaoItem = null;

        StringReader reader = new StringReader(xml);
        try {
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserFactory.newPullParser();
            parser.setInput(reader);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String startTagName = parser.getName();
                        switch (startTagName) {
                            case "items":
                                shiCaiEntry = new FindShiCaiEntry();
                                break;
                            case "code":
                                eventType = parser.next();
                                String code = parser.getText();
                                shiCaiEntry.setCode(code);
                                break;
                            case "msg":
                                eventType = parser.next();
                                String msg = parser.getText();
                                shiCaiEntry.setMsg(msg);
                                break;
                            case "total":
                                eventType = parser.next();
                                String total = parser.getText();
                                shiCaiEntry.setTotal(total);
                                break;
                            case "obj":
                                items = new ArrayList<>();
                                break;
                            case "item":
                                if (shiCaiItemEntry == null) {
                                    shiCaiItemEntry = new FindShiCaiEntry.ShiCaiItemEntry();
                                } else {
                                    eventType = parser.next();
                                    gongxiaoItem = parser.getText();
                                }
                                break;
                            case "title":
                                eventType = parser.next();
                                String title = parser.getText();
                                shiCaiItemEntry.setTitle(title);
                                break;
                            case "id":
                                eventType = parser.next();
                                String id = parser.getText();
                                shiCaiItemEntry.setId(id);
                                break;
                            case "image":
                                eventType = parser.next();
                                String image = parser.getText();
                                shiCaiItemEntry.setImage(image);
                                break;
                            case "heat_level":
                                eventType = parser.next();
                                String heat_level = parser.getText();
                                shiCaiItemEntry.setHeat_level(heat_level);
                                break;
                            case "heat_word":
                                eventType = parser.next();
                                String heat_word = parser.getText();
                                shiCaiItemEntry.setHeat_word(heat_word);
                                break;
                            case "gongxiao":
                                if (gongxiao == null) {
                                    gongxiao = new ArrayList<>();
                                }
                                break;
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTagName = parser.getName();
                        switch (endTagName) {
                            case "obj":
                                shiCaiEntry.setItems(items);
                                break;
                            case "item":
                                if (shiCaiItemEntry != null) {
                                    gongxiao.add(gongxiaoItem);
                                    gongxiaoItem = null;
                                }
                                break;
                            case "gongxiao":
                                shiCaiItemEntry.setGongxiao(gongxiao);
                                gongxiao = null;
                                items.add(shiCaiItemEntry);
                                shiCaiItemEntry = null;
                                break;
                        }
                        break;
                }
                eventType = parser.next();
            }
            return shiCaiEntry;

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static FindCaiDanEntry xml2FindCaiDanEntry(String xml) {

        FindCaiDanEntry caiDanEntry = null;
        List<FindCaiDanEntry.Data> datas = null;
        FindCaiDanEntry.Data dataItem = null;
        FindCaiDanEntry.Data.UserInfo userInfo = null;
        List<String> photos = null;
        String photosItem = null;

        StringReader reader = new StringReader(xml);
        try {
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserFactory.newPullParser();
            parser.setInput(reader);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String startTagName = parser.getName();
                        switch (startTagName) {
                            case "result":
                                if (caiDanEntry == null) {
                                    caiDanEntry = new FindCaiDanEntry();
                                } else if (photos != null) {
                                    eventType = parser.next();
                                    String photoResult = parser.getText();
                                    photos.add(photoResult);
                                } else if (datas != null) {
                                    dataItem = new FindCaiDanEntry.Data();
                                }
                                break;
                            case "total":
                                eventType = parser.next();
                                String total = parser.getText();
                                caiDanEntry.setTotal(total);
                                break;
                            case "is_have_vendor":
                                eventType = parser.next();
                                String is_have_vendor = parser.getText();
                                caiDanEntry.setIs_have_vendor(is_have_vendor);
                                break;
                            case "data":
                                datas = new ArrayList<>();
                                break;
                            case "id":
                                eventType = parser.next();
                                String id = parser.getText();
                                dataItem.setId(id);
                                break;
                            case "title":
                                eventType = parser.next();
                                String title = parser.getText();
                                dataItem.setTitle(title);
                                break;
                            case "user_info":
                                userInfo = new FindCaiDanEntry.Data.UserInfo();
                                break;
                            case "signature":
                                eventType = parser.next();
                                String signature = parser.getText();
                                userInfo.setSignature(signature);
                                break;
                            case "if_v":
                                eventType = parser.next();
                                String if_v = parser.getText();
                                userInfo.setIf_v(if_v);
                                break;
                            case "user_id":
                                eventType = parser.next();
                                String user_id = parser.getText();
                                userInfo.setUser_id(user_id);
                                break;
                            case "user_name":
                                eventType = parser.next();
                                String user_name = parser.getText();
                                userInfo.setUser_name(user_name);
                                break;
                            case "avatar":
                                eventType = parser.next();
                                String avatar = parser.getText();
                                userInfo.setAvatar(avatar);
                                break;
                            case "photos":
                                photos = new ArrayList<>();
                                break;
                            case "descr":
                                eventType = parser.next();
                                String descr = parser.getText();
                                dataItem.setDescr(descr);
                                break;
                            case "recipe_num":
                                eventType = parser.next();
                                String recipe_num = parser.getText();
                                dataItem.setRecipe_num(recipe_num);
                                break;
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTagName = parser.getName();
                        switch (endTagName) {
                            case "result":
                                break;
                            case "data":
                                caiDanEntry.setData(datas);
                                datas = null;
                                break;
                            case "user_info":
                                dataItem.setUser_info(userInfo);
                                userInfo = null;
                                break;
                            case "photos":
                                dataItem.setPhotos(photos);
                                photos = null;
                                break;
                            case "recipe_num":
                                if (datas != null) {
                                    datas.add(dataItem);
                                    dataItem = null;
                                }
                                break;
                        }
                        break;
                }
                eventType = parser.next();
            }
            return caiDanEntry;

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static FindZhuanTiEntry xml2FindZhuanTiEntry(String xml) {

        FindZhuanTiEntry zhuanTiEntry = null;
        List<FindZhuanTiEntry.Data> data = null;
        FindZhuanTiEntry.Data dataItem = null;
        FindZhuanTiEntry.Data.UserInfo userInfo = null;

        StringReader reader = new StringReader(xml);
        try {
            XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
            XmlPullParser parser = parserFactory.newPullParser();
            parser.setInput(reader);

            int eventType = parser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        break;
                    case XmlPullParser.START_TAG:
                        String startTagName = parser.getName();
                        switch (startTagName) {
                            case "result":
                                if (zhuanTiEntry == null) {
                                    zhuanTiEntry = new FindZhuanTiEntry();
                                } else if (dataItem == null) {
                                    dataItem = new FindZhuanTiEntry.Data();
                                }
                                break;
                            case "total":
                                eventType = parser.next();
                                String total = parser.getText();
                                zhuanTiEntry.setTotal(total);
                                break;
                            case "is_have_vendor":
                                eventType = parser.next();
                                String is_have_vendor = parser.getText();
                                zhuanTiEntry.setIs_have_vendor(is_have_vendor);
                                break;
                            case "data":
                                data = new ArrayList<>();
                                break;
                            case "id":
                                eventType = parser.next();
                                String id = parser.getText();
                                dataItem.setId(id);
                                break;
                            case "title":
                                eventType = parser.next();
                                String title = parser.getText();
                                dataItem.setTitle(title);
                                break;
                            case "type":
                                eventType = parser.next();
                                String type = parser.getText();
                                dataItem.setType(type);
                                break;
                            case "tj_type":
                                eventType = parser.next();
                                String tj_type = parser.getText();
                                dataItem.setTj_type(tj_type);
                                break;
                            case "f_s_type":
                                eventType = parser.next();
                                String f_s_type = parser.getText();
                                dataItem.setF_s_type(f_s_type);
                                break;
                            case "photo":
                                eventType = parser.next();
                                String photo = parser.getText();
                                dataItem.setPhoto(photo);
                                break;
                            case "desc":
                                eventType = parser.next();
                                String desc = parser.getText();
                                dataItem.setDesc(desc);
                                break;
                            case "descr":
                                eventType = parser.next();
                                String descr = parser.getText();
                                dataItem.setDescr(descr);
                                break;
                            case "user_info":
                                userInfo = new FindZhuanTiEntry.Data.UserInfo();
                                break;
                            case "signature":
                                eventType = parser.next();
                                String signature = parser.getText();
                                userInfo.setSignature(signature);
                                break;
                            case "if_v":
                                eventType = parser.next();
                                String if_v = parser.getText();
                                userInfo.setIf_v(if_v);
                                break;
                            case "user_id":
                                eventType = parser.next();
                                String user_id = parser.getText();
                                userInfo.setUser_id(user_id);
                                break;
                            case "user_name":
                                eventType = parser.next();
                                String user_name = parser.getText();
                                userInfo.setUser_name(user_name);
                            case "avatar":
                                eventType = parser.next();
                                String avatar = parser.getText();
                                userInfo.setAvatar(avatar);
                                break;
                            case "create_time":
                                eventType = parser.next();
                                String create_time = parser.getText();
                                dataItem.setCreate_time(create_time);
                                break;
                            case "zan_num":
                                eventType = parser.next();
                                String zan_num = parser.getText();
                                dataItem.setZan_num(zan_num);
                                break;
                            case "fav_num":
                                eventType = parser.next();
                                String fav_num = parser.getText();
                                dataItem.setFav_num(fav_num);
                                break;
                            case "isshow_fav":
                                eventType = parser.next();
                                String isshow_fav = parser.getText();
                                dataItem.setIsshow_fav(isshow_fav);
                                break;
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        String endTagName = parser.getName();
                        switch (endTagName) {
                            case "result":
                                break;
                            case "data":
                                zhuanTiEntry.setData(data);
                                data = null;
                                break;
                            case "user_info":
                                dataItem.setUser_info(userInfo);
                                userInfo = null;
                                break;
                            case "isshow_fav":
                                if (data != null) {
                                    data.add(dataItem);
                                    dataItem = null;
                                }
                                break;
                        }
                        break;
                }
                eventType = parser.next();
            }
            return zhuanTiEntry;

        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
